package com.xingdian.xingdianhulianserver.util.ossUtil;

import com.aliyun.oss.OSSClient;
import com.xingdian.xingdianhulianserver.common.ServerResponse;
import com.xingdian.xingdianhulianserver.util.ossUtil.storage.AliStorage;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.UUID;

public class UploadUtil {

    public static String uploadImage(File file) {
        String uploadImageKey = null;
        byte[] data = null;
        if (!file.exists()) {
            try {
                throw new FileNotFoundException("file文件不存在");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
        try {
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
            byte[] b = new byte[1000];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            data = bos.toByteArray();
            bos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        String imageName = UUID.randomUUID().toString().replaceAll("-", "");
        uploadImageKey = AliStorage.uploadImage("shopInfo/imageCode/" + imageName + ".jpg",data);
        uploadImageKey = "https://oss-huantou.oss-cn-beijing.aliyuncs.com/" + uploadImageKey;
        return uploadImageKey;
    }


    //单文件上传  商品图片
    public static String upload(MultipartFile uploadFile){
        String uploadImageKey=null;
        try {
            if(uploadFile.isEmpty()){
                return ServerResponse.createByErrorCodeMessage(101,"请选择要上传的文件").toString();
            }
            byte[] bytes = uploadFile.getBytes();//获取文件的字节数组
            String fileName=uploadFile.getOriginalFilename();//获取文件名 例如xx/dsfsd/dsfs.xxx
            String fileType= uploadFile.getContentType();//获取文件类型
            long fileSize=uploadFile.getSize();//获取文件大小

            String imageName = UUID.randomUUID().toString().replaceAll("-","");
            uploadImageKey = AliStorage.uploadImage("webimg/products/"+imageName + ".png",bytes);
            uploadImageKey = "https://oss-huantou.oss-cn-beijing.aliyuncs.com/"+uploadImageKey;
        }catch (Exception e){
        }

        return uploadImageKey;
    }

    //上传加盟品牌相关图片
    public static String upload2(MultipartFile uploadFile){
        String uploadImageKey=null;
        try {
            if(uploadFile.isEmpty()){
                return ServerResponse.createByErrorCodeMessage(101,"请选择要上传的文件").toString();
            }
            byte[] bytes = uploadFile.getBytes();//获取文件的字节数组
            String fileName=uploadFile.getOriginalFilename();//获取文件名 例如xx/dsfsd/dsfs.xxx
            String fileType= uploadFile.getContentType();//获取文件类型
            long fileSize=uploadFile.getSize();//获取文件大小

            String imageName = UUID.randomUUID().toString().replaceAll("-","");
            uploadImageKey = AliStorage.uploadImage("webimg/project/"+imageName + ".png",bytes);
            uploadImageKey = "https://oss-huantou.oss-cn-beijing.aliyuncs.com/"+uploadImageKey;
        }catch (Exception e){
        }

        return uploadImageKey;
    }

    //上传加盟品牌详情相关图片
    public static String upload3(MultipartFile uploadFile){
        String uploadImageKey=null;
        try {
            if(uploadFile.isEmpty()){
                return ServerResponse.createByErrorCodeMessage(101,"请选择要上传的文件").toString();
            }
            byte[] bytes = uploadFile.getBytes();//获取文件的字节数组
            String fileName=uploadFile.getOriginalFilename();//获取文件名 例如xx/dsfsd/dsfs.xxx
            String fileType= uploadFile.getContentType();//获取文件类型
            long fileSize=uploadFile.getSize();//获取文件大小

            String imageName = UUID.randomUUID().toString().replaceAll("-","");
            uploadImageKey = AliStorage.uploadImage("webimg/detail/"+imageName + ".png",bytes);
            uploadImageKey = "https://oss-huantou.oss-cn-beijing.aliyuncs.com/"+uploadImageKey;
        }catch (Exception e){
        }

        return uploadImageKey;
    }

    //上传加盟品牌主图
    public static String upload4(MultipartFile uploadFile){
        String uploadImageKey=null;
        try {
            if(uploadFile.isEmpty()){
                return ServerResponse.createByErrorCodeMessage(101,"请选择要上传的文件").toString();
            }
            byte[] bytes = uploadFile.getBytes();//获取文件的字节数组
            String fileName=uploadFile.getOriginalFilename();//获取文件名 例如xx/dsfsd/dsfs.xxx
            String fileType= uploadFile.getContentType();//获取文件类型
            long fileSize=uploadFile.getSize();//获取文件大小

            String imageName = UUID.randomUUID().toString().replaceAll("-","");
            uploadImageKey = AliStorage.uploadImage("webimg/productImg/"+imageName + ".png",bytes);
            uploadImageKey = "https://oss-huantou.oss-cn-beijing.aliyuncs.com/"+uploadImageKey;
        }catch (Exception e){
        }

        return uploadImageKey;
    }

    public static ServerResponse<Object> upLoadShopImg(MultipartFile file) {

            OSSClient ossClient = OSSUtil.getOSSClient();

        String uploadImageKey = null;
        try {
            if (file.isEmpty()) {
                return ServerResponse.createByErrorCodeMessage(102, "没有要上传的图片");
            }

            byte[] bytes = new byte[0];//获取文件的字节数组
            bytes = file.getBytes();
            String fileName = file.getOriginalFilename();//获取文件名 例如xx/dsfsd/dsfs.xxx
            String fileType = file.getContentType();//获取文件类型
            long fileSize = file.getSize();//获取文件大小
            String imageName = UUID.randomUUID().toString().replaceAll("-","");
            uploadImageKey = AliStorage.uploadImage("shopInfo/image/"+imageName+ ".jpg",bytes);
            String baseImgUrl = "http://jinggui.oss-cn-beijing.aliyuncs.com/"+uploadImageKey;
            return ServerResponse.createBySuccess(baseImgUrl);
        } catch (Exception e) {
            return ServerResponse.createByErrorCodeMessage(104, "图片上传异常");
        }

    }

    public static ServerResponse<Object> upload1(MultipartFile uploadFile){
        String uploadImageKey=null;
        try {
            if(uploadFile.isEmpty()){
                return ServerResponse.createByErrorCodeMessage(101,"请选择要上传的文件");
            }
            byte[] bytes = uploadFile.getBytes();//获取文件的字节数组
            String fileName=uploadFile.getOriginalFilename();//获取文件名 例如xx/dsfsd/dsfs.xxx
            String fileType= uploadFile.getContentType();//获取文件类型
            long fileSize=uploadFile.getSize();//获取文件大小

            String imageName = UUID.randomUUID().toString().replaceAll("-","");
            uploadImageKey = AliStorage.uploadImage("shopInfo/image/"+imageName + ".jpg",bytes);
            uploadImageKey = "https://oss-huantou.oss-cn-beijing.aliyuncs.com/"+uploadImageKey;
        }catch (Exception e){
        }

        return ServerResponse.createBySuccess(uploadImageKey);
    }

    //根据key删除文件
    public static ServerResponse deleteImg(String url){
//        OSSUtil.deleteFile();
        OSSClient ossClient = OSSUtil.getOSSClient();
        OSSUtil.deleteFile(ossClient,"jinggui",url);
        return ServerResponse.createBySuccess("删除成功!");
    }
}
